package com.weiyu.bultrasound.core.model.common;

import java.io.Serializable;
import java.util.Map;

/**
 *@description 
 *@author abner chow
 *@date 2019-08-08 17:09
 */
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = -3458066472643731732L;

    /**
     * 状态码
     */
    private Integer status;
    /**
     * 返回的消息
     */
    private String message;

    /**
     * 数据内容体
     */
    private T content;


    public static <E> JsonResult<E> getResult(StatusCode code, E data) {
        JsonResult<E> result = new JsonResult<E>();
        result.setStatus(code.getCode());
        result.setMessage(code.getMeaning());
        result.setContent(data);
        return result;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }


    public static <E> JsonResult<E> getSuccessResult() {
        return getResult(StatusCode.OK, null);
    }

    public static <E> JsonResult<E> getSuccessResult(E data) {
        return getResult(StatusCode.OK, data);
    }

    public static <E> JsonResult<E> getFailureResult() {
        return getResult(StatusCode.SERVER_ERROR, null);
    }

    /**
     * @description 根据错误状态枚举类型返回
     * @Param: [code, data]
     * @Return: com.weiyu.bultrasound.core.model.common.JsonResult<E>
     * @Author: abner chow
     * @Date: 2019/8/8 17:19
     */
    public static <E> JsonResult<E> getFailureResult(StatusCode code, E data) {
        JsonResult<E> result = new JsonResult<E>();
        result.setStatus(code.getCode());
        result.setMessage(code.getMeaning());
        result.setContent(data);
        return result;
    }

    /**
     * @description 自定义错误信息
     * @Param: [errmsg]
     * @Return: com.weiyu.bultrasound.core.model.common.JsonResult<E>
     * @Author: abner chow
     * @Date: 2019/8/8 17:19
     */
    public static <E> JsonResult<E> getFailureResult(String errmsg) {
        JsonResult<E> result = new JsonResult<E>();
        result.setStatus(StatusCode.SERVER_ERROR.getCode());
        result.setMessage(errmsg);
        result.setContent(null);
        return result;
    }

    /**
     * @description 返回map类型的结果
     * @Param: [data]
     * @Return: com.weiyu.bultrasound.core.model.common.JsonResult<java.lang.Object>
     * @Author: abner chow
     * @Date: 2019/8/8 17:24
     */
    public static JsonResult<Object> getResult4Map(Map<String, Object> data) {
        JsonResult<Object> returnData = new JsonResult<>();
        //返回为空 返回400
        if (data == null || data.size()==0) {
            returnData.setStatus(StatusCode.INVALID_REQUEST.getCode());
            returnData.setMessage(StatusCode.INVALID_REQUEST.getMeaning());
            returnData.setContent(data);
            return returnData;
        }
        //未包含错误码 返回200
        if (!data.containsKey("statusCode")) {
            returnData.setStatus(StatusCode.OK.getCode());
            returnData.setMessage(StatusCode.OK.getMeaning());
            returnData.setContent(data);
            return returnData;
        }else if(data.containsKey("statusCode")){
            returnData.setStatus( Integer.parseInt(data.get("statusCode").toString()));
            returnData.setMessage(data.get("statusMessage").toString());
            returnData.setContent("");
        }
        return returnData;
    }


}
